<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Core;
use app\admin\model\Monitor;
use app\admin\model\Monitor_alarm;
use app\admin\model\Monitor_alarm_history;
use app\admin\model\Room_loop;
use app\admin\model\Email;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use think\Paginator;
use think\Session;
use \think\Loader;
use think\Collection;
use think\Model;

class Coring extends Base
{
	public function __construct(){
		parent::__construct();
		$this->core = new Core();
	    $this->monitor = new Monitor();
	    $this->monitor_alarm = new Monitor_alarm();
	    $this->alarm_history = new Monitor_alarm_history();
	    $this->room_loop = new Room_loop();
	}

	// 实时监测index
	public function index() {
		if(session('userinfo')) {
			// 取出实时检测表
			$alltime_info = $this->core->paginate(8);
			$monitor_info = $this->monitor->paginate(8);

			$this->assign('monitor_info',$monitor_info);
			$this->assign('alltime_info',$alltime_info);
			// return json_encode($alltime_info);

		    return $this->fetch();
		}else {
			return $this->error('请登录',url('Admin/admin/login'));
		}
	}

	// 
	public function getdata(){
		$log =[];
		// 取出测点定义信息
  		$this->coretemp = Loader::model('Core');
		$alltime_info = $this->coretemp->select();
		// 根据core_id，从monitor表中取出每个回路的额定电流温度值，
		// 判断报警状态，加入monitor_alarm表中
		foreach($alltime_info as $alltime_info_item) {
			$temp_core_id = $alltime_info_item['core_id'];
			// dump($temp_core_id);die;
			// 取出回路名称,id以及上级电气室负责人邮箱
			$core_id_all = $this->room_loop->where('loop_id',$temp_core_id)->find();
			// dump($core_id_all);die;
			$loop_name = $core_id_all['loop_name'];
			$room_id = $core_id_all['room_id'];
			$roomer_email = $core_id_all['roomer_email'];
			// dump($temp_core_id);die;
			$temp_current = $alltime_info_item['resi_current'];
			// dump($temp_current);die;
			$temp_tempure = $alltime_info_item['cable_temp'];
			// dump($temp_tempure);die;
			$all_monitor = $this->monitor->column('monitor_id');
			// dump($all_monitor);die;
			if(in_array($temp_core_id, $all_monitor)) {
				// dump(in_array($temp_core_id, $all_monitor));die;
				$normal_current  = $this->monitor->where('monitor_id',$temp_core_id)->column('over_current');
				// dump($normal_current);die;
				$normal_tempure  = $this->monitor->where('monitor_id',$temp_core_id)->column('cable_temp');
				$normal_near_infrared  = $this->monitor->where('monitor_id',$temp_core_id)->column('near_infrared');
				// dump($normal_tempure);die;
				// dump($temp_tempure > $normal_tempure[0]);die;
				$res1 = $temp_tempure > $normal_tempure[0];
				// dump($res1);die;
				$now_time = $this->dataNow();
				$new_record['start_time'] = $this->dataNow();
				$new_record['dura_time'] = NULL;
				$new_record['user_id'] = $this->current_user['username'];
				// $new_record['operation'] = NULL;
				$new_record['confirm_time'] = '5';
				$new_record['monitor_id'] = $temp_core_id;
				$last_record_all = $this->monitor_alarm->where('monitor_id',$temp_core_id)->column('start_time');
				// $last_record = toArr
				$count = count($last_record_all);
				// dump($count);die;
				$last_num = $count;
				// dump($last_record_all);die;
				$last_record = $this->monitor_alarm->where('start_time',$last_record_all[$last_num-1])->find();
				// dump($last_record);die;
				$last_record_status = $last_record['status'];
				// dump($last_record['status']);die;
				// dump($last_srecord_status);die;
				$near_infrared_num = $alltime_info_item['near_infrared'];
				$alltime_info_item['near'] = false;
				$near_res = $near_infrared_num > $normal_near_infrared[0];
				if($res1) {
					$alltime_info_item['status'] =  2;					
				   if($last_record_status == 3) {
					$new_record['alarm_type'] = '0';
					// $alltime_info_item['near'] = false;
					$new_record['status'] = 0;
					// $new_record['mobile_phone'] = 0;
					// dump($new_record);die;
					// $res1 = $this->monitor_alarm->insert($new_record);
					$res1 = $this->monitor_alarm->where('monitor_id',$temp_core_id)->update(['start_time' => $now_time,'alarm_type' => '0','status' => '0','mobile_phone' => '0']);	
					$res2 =  $this->alarm_history->insert($new_record);
					$this->auto_email($temp_core_id,$loop_name,$room_id,'温度',$roomer_email);
					dump($res1);die;						
					}
				}
				elseif($alltime_info_item['smoke'] == 1) {
					$alltime_info_item['status'] =  2;					
				   if($last_record_status == 3) {
					$new_record['alarm_type'] = '1';
					// $alltime_info_item['near'] = false;
					$new_record['status'] = 0;
					// $new_record['mobile_phone'] = 0;
					// dump($new_record);die;
					// $res1 = $this->monitor_alarm->insert($new_record);
					$res1 = $this->monitor_alarm->where('monitor_id',$temp_core_id)->update(['start_time' => $now_time,'alarm_type' => '1','status' => '0','mobile_phone' => '0']);	
					$res2 =  $this->alarm_history->insert($new_record);
					$this->auto_email($temp_core_id,$loop_name,$room_id,'烟雾',$roomer_email);
					// dump($res1);die;						
					}
				}
				elseif($alltime_info_item['fire'] == 1) {
				   $alltime_info_item['status'] =  2;					
				   if($last_record_status == 3) {
					$new_record['alarm_type'] = '4';
					// $alltime_info_item['near'] = false;
					$new_record['status'] = 0;
					// $new_record['mobile_phone'] = 0;
					// dump($new_record);die;
					// $res1 = $this->monitor_alarm->insert($new_record);
					$res1 = $this->monitor_alarm->where('monitor_id',$temp_core_id)->update(['start_time' => $now_time,'alarm_type' => '4','status' => '0','mobile_phone' => '0']);	
					$res2 =  $this->alarm_history->insert($new_record);
					$this->auto_email($temp_core_id,$loop_name,$room_id,'火灾大小',$roomer_email);
					// dump($res1);die;						
					}
				}
				elseif($near_res) {

				   $alltime_info_item['status'] =  2;
				   $alltime_info_item['near'] = true;
				   if($last_record_status == 3) {
					$new_record['alarm_type'] = '5';
					$new_record['status'] = 0;
					// $new_record['mobile_phone'] = 0;
					// dump($new_record);die;
					// $res1 = $this->monitor_alarm->insert($new_record);
					$res1 = $this->monitor_alarm->where('monitor_id',$temp_core_id)->update(['start_time' => $now_time,'alarm_type' => '5','status' => '0','mobile_phone' => '0']);	
					$res2 =  $this->alarm_history->insert($new_record);
					$this->auto_email($temp_core_id,$loop_name,$room_id,'近红外图像检测异常！',$roomer_email);
					// dump($res1);die;						
					}					
				}

				// $res2 = $temp_current > $normal_current[0];
				// dump($res2);die;
				elseif(($temp_current > $normal_current[0])) {
					// $res2 = $this->monitor_alarm->where('monitor_id',$temp_core_id)->update(['alarm_type' => '1','status' => '0','mobile_phone' => '0']);
					// // dump($res2);die;				
					// // $this->auto_email($temp_core_id,$loop_name,$room_id,'电流',$roomer_email);
					// $alltime_info_item['status'] =  2;
				}
				// dump(($temp_tempure > $normal_tempure[0]) && ($temp_current > $normal_current[0]));die;
				elseif(($temp_tempure > $normal_tempure[0]) && ($temp_current > $normal_current[0])) {
					// $res3 = $this->monitor_alarm->where('monitor_id',$temp_core_id)->update(['alarm_type' => '2','status' => '0','mobile_phone' => '0']);	
					// // dump($res3);die;
					// // $this->auto_email($temp_core_id,$loop_name,$room_id,'温度和电流',$roomer_email);
					// $alltime_info_item['status'] =  2;
				}
				elseif(($temp_tempure < $normal_tempure[0]) && ($temp_current < $normal_current[0])) {
					// $res3 = $this->monitor_alarm->where('monitor_id',$temp_core_id)->update(['alarm_type' => '3','status' => '3','mobile_phone' => '1']);						
				}else {

				}				
			}

		}		
		//  $this->coretemp = Loader::model('Core');
		// $alltime_info = $this->coretemp->select();

		// $alltime_info = collection($alltime_info)->toArray();
		// dump($alltime_info);die;
		// $alltime_info = collection($alltime_info)->toArray();
		// array_push($log, $alltime_info);
		// return $alltime_info;
		// dump(toJson($alltime_info));die;
		// return json_encode($alltime_info);
		// echo $alltime_info;
		//$this->ajaxReturn($alltime_info);
		return json_encode($alltime_info);
	}

	public function getdata_second() {
		$log =[];
  		$this->coretemp = Loader::model('Core');
		$alltime_info = $this->coretemp->select();
		return json_encode($alltime_info);		
	}
	// 自动发送邮件
	public function auto_email($monitor_id,$loop_name,$location,$alarm_type,$email_address) {
				// 实例化PHPMailer类
                $mail = new PHPMailer(true);
                // 服务器设置
                $mail->SMTPDebug = 2; // 开启Debug
                $mail->isSMTP(); // 使用SMTP
                $mail->Host = 'smtp.qq.com'; // 服务器地址
                $mail->SMTPAuth = true; // 开启SMTP验证
                $mail->Username = '1451171391@qq.com'; // SMTP 用户名
                $mail->Password = 'vykmmvaoamgqgcbj'; // SMTP 密码
                $mail->SMTPSecure = 'ssl'; // 开启TLS 可选
                $mail->Port = 465; // 端口
                // 收件人
                $mail->setFrom('1451171391@qq.com'); // 来自
                $mail->addAddress($email_address); // 添加一个收件人
                //$mail->addAddress('395696661@qq.com'); // 可以只传邮箱地址
                // 附件
                //$mail->addAttachment('1.jpg'); // 添加附件
                //$mail->addAttachment('2.zip'); // 可以设定名字
                // 内容
                $mail->isHTML(true); // 设置邮件格式为HTML
                $mail->CharSet='UTF-8';
                $mail->Subject = '电气火灾消防预警管理平台——报警通知'; //邮件主题
                $mail->Body = "<h3>实时报警点：<h3 style='color:red;'>{$monitor_id} / {$loop_name}</h3></h3><br/><h3>报警位置：<h3 style='color:red;'>[工厂1] [车间1] [工段1] {$location}电气室</h3></h3><br/>
                <h3>报警类型：<h3 style='color:red;'>{$alarm_type}超越额定值！</h3></h3><br>
                "; //邮件内容
                $res = $mail->send();
                if($res) {
                    $email_con['sender'] = $mail->Username;
                    $email_con['receiver'] = $data['address'];
                    $email_con['monitor_id'] = $data['monitor_id'];
                    $email_con['loop_name'] = $loop_name;
                    $email_con['content'] = $mail->Body;
                    $email_con['alarm_type'] = $data['alarm_type'];
                    $email_con['time'] = $this->dataNow();
                    $res_temp = $this->email->save($email_con);
					}
				}

}

